package com.qupai.lib_base.utils

import android.graphics.*
import android.util.Log
import com.blankj.utilcode.util.ImageUtils
import com.blankj.utilcode.util.ObjectUtils

object SpliceBitmapUtils {

    fun cropImage(originalBitmap: Bitmap, targetWidth: Int, targetHeight: Int): Bitmap {

        val srcWidth = originalBitmap.width
        val srcHeight = originalBitmap.height

        val scaleWidth = targetWidth.toFloat() / srcWidth
        val scaleHeight = targetHeight.toFloat() / srcHeight
        val scaleFactor = scaleWidth.coerceAtMost(scaleHeight)

        val scaledWidth = (srcWidth * scaleFactor).toInt()
        val scaledHeight = (srcHeight * scaleFactor).toInt()

        val matrix = Matrix()
        matrix.postScale(scaleFactor, scaleFactor)

        val scaledBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.width, originalBitmap.height, matrix, true)
        originalBitmap.recycle()

        val xOffset = (scaledWidth - targetWidth) / 2
        val yOffset = (scaledHeight - targetHeight) / 2

        return Bitmap.createBitmap(scaledBitmap, xOffset, yOffset, targetWidth, targetHeight)
    }

    fun imageToGif(gifPathLists: ArrayList<String>) {
        when (gifPathLists.size) {
            1 -> {

            }

            2 -> {

            }

            3 -> {

            }

            4 -> {
                imageToUtil(
                    ImageUtils.getBitmap(gifPathLists[0]),
                    ImageUtils.getBitmap(gifPathLists[1]),
                    ImageUtils.getBitmap(gifPathLists[2]),
                    ImageUtils.getBitmap(gifPathLists[3])
                )
            }

            5 -> {

            }

            6 -> {

            }

            7 -> {

            }

            8 -> {

            }

            9 -> {

            }

            10 -> {

            }
        }
    }

    /**
     * 制作一张画布，将四小图合成大图
     */
    fun imageToUtil(leftTopBitmap: Bitmap, rightTopBitmap: Bitmap, leftBottomBitmap: Bitmap, rightBottomBitmap: Bitmap): Bitmap? {
        val newBitmap = Bitmap.createBitmap(1020, 740, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(newBitmap)
        val paint = Paint()
        paint.color = Color.WHITE
        paint.textSize = 35f
        paint.style = Paint.Style.FILL
        val fontMetrics = paint.fontMetrics
        canvas.drawText("All in love", 50f, 45f, paint) //文字完全显示
        //        canvas.drawText("All in love", 0, 15, paint);//文字会显示不全
        //480*320
        canvas.drawBitmap(leftTopBitmap, null, Rect(20, 60, 500, 380), null)
        canvas.drawBitmap(rightTopBitmap, null, Rect(520, 60, 1000, 380), null)
        canvas.drawBitmap(leftBottomBitmap, null, Rect(20, 400, 500, 720), null)
        canvas.drawBitmap(rightBottomBitmap, null, Rect(520, 400, 1000, 720), null)
        try {
            leftTopBitmap.recycle()
            rightTopBitmap.recycle()
            leftBottomBitmap.recycle()
            rightBottomBitmap.recycle()
        } catch (e: Exception) {
        }
        return newBitmap
    }

    /*
     * 把两个位图覆盖合成为一个位图，以底层位图的长宽为基准
     * @param backBitmap 在底部的位图
     * @param frontBitmap 盖在上面的位图
     * @return
     */
    fun mergeBitmap(backBitmap: Bitmap?, frontBitmap: Bitmap?): Bitmap? {
        if (backBitmap == null || backBitmap.isRecycled() || frontBitmap == null || frontBitmap.isRecycled()) {
            Log.e("yitu", "backBitmap=$backBitmap;frontBitmap=$frontBitmap")
            return null
        }
        val bitmap: Bitmap = backBitmap.copy(Bitmap.Config.ARGB_8888, true)
        val canvas = Canvas(bitmap)
        val baseRect = Rect(0, 0, backBitmap.getWidth(), backBitmap.getHeight())
        val frontRect = Rect(0, 0, frontBitmap.getWidth(), frontBitmap.getHeight())
        canvas.drawBitmap(frontBitmap, frontRect, baseRect, null)
        return bitmap
    }

    /**
     * 把两个位图覆盖合成为一个位图，左右拼接
     *
     * @param leftBitmap
     * @param rightBitmap
     * @return
     */
    fun mergeBitmap_LR(leftBitmap: Bitmap?, rightBitmap: Bitmap?): Bitmap? {
        if (leftBitmap == null || leftBitmap.isRecycled() || rightBitmap == null || rightBitmap.isRecycled()) {
            return null
        }
        // 缩放之后的bitmap
        // 拼接后的宽度
        val height: Int = leftBitmap.getHeight()
        val width: Int = leftBitmap.getWidth() * 2
        // 定义输出的bitmap
        val bitmap: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        // 缩放后两个bitmap需要绘制的参数
        val leftRect = Rect(0, 0, leftBitmap.getWidth(), leftBitmap.getHeight())
        val rightRect = Rect(0, 0, leftBitmap.getWidth(), leftBitmap.getHeight())
        // 右边图需要绘制的位置，往右边偏移左边图的宽度，高度是相同的
        val rightRectT = Rect(leftBitmap.getWidth(), 0, width, height)
        canvas.drawBitmap(leftBitmap, leftRect, leftRect, null)
        canvas.drawBitmap(rightBitmap, rightRect, rightRectT, null)
        if (ObjectUtils.isEmpty(leftBitmap)) {
            leftBitmap.recycle()
        }
        if (ObjectUtils.isEmpty(rightBitmap)) {
            rightBitmap.recycle()
        }
        return bitmap
    }

    /**
     * 把两个同样位图覆盖合成为一个位图，左右拼接
     *
     * @param leftBitmap
     * @param rightBitmap
     * @return
     */
    fun mergeBitmap_LR(leftBitmap: Bitmap?): Bitmap? {
        if (leftBitmap == null || leftBitmap.isRecycled()) {
            return null
        }
        // 缩放之后的bitmap
        // 拼接后的宽度
        val height: Int = leftBitmap.getHeight()
        val width: Int = leftBitmap.getWidth() * 2
        // 定义输出的bitmap
        val bitmap: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        // 缩放后两个bitmap需要绘制的参数
        val leftRect = Rect(0, 0, leftBitmap.getWidth(), leftBitmap.getHeight())
        val rightRect = Rect(0, 0, leftBitmap.getWidth(), leftBitmap.getHeight())
        // 右边图需要绘制的位置，往右边偏移左边图的宽度，高度是相同的
        val rightRectT = Rect(leftBitmap.getWidth(), 0, width, height)
        canvas.drawBitmap(leftBitmap, leftRect, leftRect, null)
        canvas.drawBitmap(leftBitmap, rightRect, rightRectT, null)
//        if (ObjectUtils.isEmpty(leftBitmap)) {
//            leftBitmap.recycle()
//        }
        return bitmap
    }

    fun mergeBitmapList_LR(photoPathList: List<String?>): List<Bitmap?> {
        val originalBitmapList: MutableList<Bitmap> = ArrayList<Bitmap>()
        val resultBitmapList: MutableList<Bitmap?> = ArrayList<Bitmap?>()
        for (path in photoPathList) {
            originalBitmapList.add(ImageUtils.getBitmap(path))
        }
        //奇数，需要补一张空白图
        if (photoPathList.size % 2 != 0) {
//            originalBitmapList.add(ImageUtils.getBitmap(R.drawable.temp))
        }
        //预设一个不大于2的list
        val twoBitmaps: MutableList<Bitmap> = ArrayList<Bitmap>()
        for (bitmap in originalBitmapList) {
            twoBitmaps.add(bitmap)
            //如果有两张了，就开始合成
            if (twoBitmaps.size == 2) {
                resultBitmapList.add(mergeBitmap_LR(twoBitmaps[0], twoBitmaps[1]))
                twoBitmaps.clear()
            }
        }
        return resultBitmapList
    }

    /**
     * 把两个位图覆盖合成为一个位图，上下拼接
     *
     * @param topBitmap
     * @param bottomBitmap
     * @param isBaseMax    是否以高度大的位图为准，true则小图等比拉伸，false则大图等比压缩
     * @return
     */
    fun mergeBitmap_TB(topBitmap: Bitmap?, bottomBitmap: Bitmap?, isBaseMax: Boolean): Bitmap? {
        if (topBitmap == null || topBitmap.isRecycled() || bottomBitmap == null || bottomBitmap.isRecycled()) {
            return null
        }
        var width = 0
        width = if (isBaseMax) {
            if (topBitmap.getWidth() > bottomBitmap.getWidth()) topBitmap.getWidth() else bottomBitmap.getWidth()
        } else {
            if (topBitmap.getWidth() < bottomBitmap.getWidth()) topBitmap.getWidth() else bottomBitmap.getWidth()
        }
        var tempBitmapT: Bitmap = topBitmap
        var tempBitmapB: Bitmap = bottomBitmap
        if (topBitmap.getWidth() != width) {
            tempBitmapT = Bitmap.createScaledBitmap(topBitmap, width, (topBitmap.getHeight() * 1f / topBitmap.getWidth() * width).toInt(), false)
        } else if (bottomBitmap.getWidth() != width) {
            tempBitmapB =
                Bitmap.createScaledBitmap(bottomBitmap, width, (bottomBitmap.getHeight() * 1f / bottomBitmap.getWidth() * width).toInt(), false)
        }
        val height: Int = tempBitmapT.getHeight() + tempBitmapB.getHeight()
        val bitmap: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        val topRect = Rect(0, 0, tempBitmapT.getWidth(), tempBitmapT.getHeight())
        val bottomRect = Rect(0, 0, tempBitmapB.getWidth(), tempBitmapB.getHeight())
        val bottomRectT = Rect(0, tempBitmapT.getHeight(), width, height)
        canvas.drawBitmap(tempBitmapT, topRect, topRect, null)
        canvas.drawBitmap(tempBitmapB, bottomRect, bottomRectT, null)
        return bitmap
    }

    /**
     * 将bitmap指定区域设置为透明
     *
     * @param b
     * @param width  透明区域宽度
     * @param height 透明区域高度
     * @param x      透明区域起始x坐标
     * @param y      透明区域起始y坐标
     * @return
     */
    fun setTransparentAreaForBitmap(b: Bitmap?, width: Int, height: Int, x: Int, y: Int): Bitmap? {
        if (b == null) {
            return null
        }
        Log.e("frank", b.width.toString() + "--" + b.height + "     ::   " + width + " -- " + height + " == " + x + "  -- " + y)
        val bitmap = b.copy(b.config, true)
        val pix = IntArray(width * height)
        for (j in 0 until height) {
            for (i in 0 until width) {
                val index = j * width + i
                pix[index] = 0x00000000
            }
        }
        bitmap.setPixels(
            pix, 0, width, x, y, width, height
        )
        return bitmap
    }
}